package pub.developers.forum.facade.impl;

import org.springframework.stereotype.Service;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.api.model.PageResponseModel;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.AdminBooleanRequest;
import pub.developers.forum.api.request.article.*;
import pub.developers.forum.api.response.article.ArticleInfoResponse;
import pub.developers.forum.api.response.article.ArticleQueryTypesResponse;
import pub.developers.forum.api.response.article.ArticleUserPageResponse;
import pub.developers.forum.api.response.article.FilterArticlePageResponse;
import pub.developers.forum.api.response.inform.InformPageResponse;
import pub.developers.forum.api.service.ArticleApiService;
import pub.developers.forum.app.manager.ArticleManager;
import pub.developers.forum.common.enums.ErrorCodeEn;
import pub.developers.forum.domain.service.OssTxtService;
import pub.developers.forum.facade.support.ResultModelUtil;
import pub.developers.forum.facade.validator.ArticleValidator;
import pub.developers.forum.facade.validator.PageRequestModelValidator;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class ArticleApiServiceImpl implements ArticleApiService {

    @Resource
    private ArticleManager articleManager;

    @Resource
    private OssTxtService ossTxtService;


    @Override
    public ResultModel reportReview(AdminBooleanRequest request) {
        articleManager.reportReview(request);
        return ResultModelUtil.success();
    }

    @Override
    public ResultModel<FilterArticlePageResponse> queryFilterPosts(PageRequestModel pageRequestModel) throws Exception {
        return ResultModelUtil.success(articleManager.queryFilterPosts(pageRequestModel));
    }

    public ResultModel<PageResponseModel<InformPageResponse>> queryReported(PageRequestModel pageRequestModel) throws Exception {

        return ResultModelUtil.success(articleManager.queryReported(pageRequestModel));
    }

    @Override
    public ResultModel<List<ArticleInfoResponse>> queryBan(PageRequestModel pageRequestModel) {
        return ResultModelUtil.success(articleManager.queryBan(pageRequestModel));
    }

    @Override
    public ResultModel<List<ArticleQueryTypesResponse>> queryAllTypes() {
        return ResultModelUtil.success(articleManager.queryAllTypes());
    }

    @Override
    public ResultModel<List<ArticleQueryTypesResponse>> queryAdminTypes() {
        return ResultModelUtil.success(articleManager.queryAdminTypes());
    }

    @Override
    public ResultModel<List<ArticleQueryTypesResponse>> queryEditArticleTypes() {
        return ResultModelUtil.success(articleManager.queryEditArticleTypes());
    }

    @Override
    public ResultModel addType(ArticleAddTypeRequest request) {
        ArticleValidator.addType(request);

        articleManager.addType(request);

        return ResultModelUtil.success();
    }

    @Override
    public ResultModel<Long> saveArticle(ArticleSaveArticleRequest request) throws Exception {
        ArticleValidator.saveArticle(request);

        //审核帖子
        StringBuilder sb = new StringBuilder();
        sb.append(request.getContentType());
        sb.append(request.getMarkdownContent());
        sb.append(request.getHtmlContent());

        List<Map<String, String>> result = ossTxtService.checkText(sb.toString());
        if ("block".equals(result.get(0).get("suggestion"))) {
            return ResultModelUtil.fail(ErrorCodeEn.POSTS_IS_NOPASS);
        }

        return ResultModelUtil.success(articleManager.saveArticle(request));
    }

    @Override
    public ResultModel<PageResponseModel<ArticleUserPageResponse>> userPage(PageRequestModel<ArticleUserPageRequest> pageRequestModel) {
        PageRequestModelValidator.validator(pageRequestModel);

        return ResultModelUtil.success(articleManager.userPage(pageRequestModel));
    }

    @Override
    public ResultModel<PageResponseModel<ArticleUserPageResponse>> authorPage(PageRequestModel<ArticleAuthorPageRequest> pageRequestModel) {
        PageRequestModelValidator.validator(pageRequestModel);

        return ResultModelUtil.success(articleManager.authorPage(pageRequestModel));
    }

    @Override
    public ResultModel<PageResponseModel<ArticleUserPageResponse>> adminPage(PageRequestModel<ArticleAdminPageRequest> pageRequestModel) {
        PageRequestModelValidator.validator(pageRequestModel);

        return ResultModelUtil.success(articleManager.adminPage(pageRequestModel));
    }

    @Override
    public ResultModel<ArticleInfoResponse> info(Long id) throws Exception {

        return ResultModelUtil.success(articleManager.info(id));
    }

    @Override
    public ResultModel adminTop(AdminBooleanRequest booleanRequest) {
        ArticleValidator.validatorBooleanRequest(booleanRequest);

        articleManager.adminTop(booleanRequest);

        return ResultModelUtil.success();
    }

    @Override
    public ResultModel adminOfficial(AdminBooleanRequest booleanRequest) {
        ArticleValidator.validatorBooleanRequest(booleanRequest);

        articleManager.adminOfficial(booleanRequest);

        return ResultModelUtil.success();
    }

    @Override
    public ResultModel adminMarrow(AdminBooleanRequest booleanRequest) {
        ArticleValidator.validatorBooleanRequest(booleanRequest);

        articleManager.adminMarrow(booleanRequest);

        return ResultModelUtil.success();
    }

    @Override
    public ResultModel<PageResponseModel<ArticleQueryTypesResponse>> typePage(PageRequestModel<ArticleAdminTypePageRequest> pageRequestModel) {
        PageRequestModelValidator.validator(pageRequestModel);

        return ResultModelUtil.success(articleManager.typePage(pageRequestModel));
    }

    @Override
    public ResultModel typeAuditState(AdminBooleanRequest booleanRequest) {
        ArticleValidator.validatorBooleanRequest(booleanRequest);

        articleManager.typeAuditState(booleanRequest);

        return ResultModelUtil.success();
    }


    @Override
    public ResultModel delete(ArticleDeleteRequest deleteRequest) {
        ArticleValidator.deleteArticle(deleteRequest); //参数校验
        articleManager.deleteArticle(deleteRequest);
        //返回值
        return ResultModelUtil.success();
    }


    //用户获取自己发表的帖子
    @Override
    public ResultModel<List<ArticleInfoResponse>> querySelfPosts(PageRequestModel pageRequestModel,Long userId) throws Exception {
        return ResultModelUtil.success(articleManager.querySelfPostsManager(pageRequestModel,userId));
    }


    public ResultModel<List<ArticleInfoResponse>> queryAllPosts (PageRequestModel pageRequestModel) throws Exception {
        return ResultModelUtil.success(articleManager.queryAllPosts(pageRequestModel));
    }

    //查询官方帖子
    @Override
    public ResultModel<List<ArticleUserPageResponse>> queryOfficial() {
        return ResultModelUtil.success(articleManager.queryOfficial());
    }
}
